package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;


import com.itheima.pojo.Management;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;
   @Autowired
   private MemberDao memberDao;
   @Autowired
   private OrderDao orderDao;
    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);

        //调用DAO，根据日期范围查询预约设置数据
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map<String, Object> m = new HashMap<>();
                m.put("date", orderSetting.getOrderDate().getDate());
                m.put("number", orderSetting.getNumber());
                m.put("reservations", orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            orderSettingDao.add(orderSetting);
        }
    }




     @Override
     public PageResult pageQuery(QueryPageBean queryPageBean){
         Integer currentPage = queryPageBean.getCurrentPage();
         Integer pageSize = queryPageBean.getPageSize();
         String queryString = queryPageBean.getQueryString();

         ObjectMapper mapper = new ObjectMapper();


         Map<String,Object> map=new HashMap<>();
         try {
             map = mapper.readValue(queryString, Map.class);
         } catch (IOException e) {
             e.printStackTrace();
         }

         Object dates = map.get("date");
         String Strdatas = dates.toString();
         if (Strdatas !=null && Strdatas.length()>10){

             String startDate = Strdatas.substring(1, 11);
             String stopDate = Strdatas.substring(27, 37);
             map.put("startDate",startDate);
             map.put("stopDate",stopDate);
             System.out.println(dates);
             System.out.println(startDate);
             System.out.println(stopDate);
             System.out.println(map);
         }

         PageHelper.startPage(currentPage, pageSize);
         Page<Management> page = orderSettingDao.findByCondition(map);
         return new PageResult(page.getTotal(), page.getResult());
     }

    @Override
    public void add1(Management management, Integer[] checkgroupIds) {
        String date = management.getDate();
        String name = management.getName();
        String tel = management.getTel();
        String substringDate = date.substring(0, 10);

        Member findByTelephone = memberDao.findByTelephone(tel);
        if (findByTelephone != null) {
            for (Integer checkgroupId : checkgroupIds) {
                Management Management1 = new Management();
                Management1.setName(name);
                Management1.setTel(tel);
                Management1.setDate(substringDate);
                Management1.setSetmealname(checkgroupId + "");
                orderSettingDao.add1(Management1);
            }
        } else {
            Member member = new Member();
            member.setName(name);
            member.setPhoneNumber(tel);
            memberDao.add(member);

            for (Integer checkgroupId : checkgroupIds) {
                Management Management1 = new Management();
                Management1.setName(name);
                Management1.setTel(tel);
                Management1.setDate(substringDate);
                Management1.setSetmealname(checkgroupId + "");
                orderSettingDao.add1(Management1);
            }

        }
    }

    @Override
    public void delete(Integer order_id) {
        orderSettingDao.delete(order_id);
    }

    @Override
    public PageResult findByQueryString(Map<String, String> map) {
        return null;
    }

 /*   @Override
    public PageResult findByQueryString(Map<String,String> map) {
//        String orderType = (String) map.get("orderType");
//        String orderStatus = (String) map.get("orderStatus");
//        String date = (String) map.get("date");
//        String name_tel = (String) map.get("name_tel");
        PageHelper.startPage(1, 20);
        //Page<Management> page = orderSettingDao.finByQueryString(orderType,orderStatus,date,name_tel);
        Page<Management> page = orderSettingDao.finByQueryString(map);
        for (Management management : page) {
            System.out.println(management);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }*/

    @Override
    public void editStatus(Management management) {

        orderSettingDao.editStatus(management);
    }

    @Override
    public void edit(Management management) {
        orderSettingDao.edit(management);
    }

    @Override
    public Management findByOrder(Management management) {
        return orderSettingDao.findByOrder(management);
    }

    @Override
    public int findSetmealIdsByOrderId(Integer id) {
        return orderSettingDao.findSetmealIdsByOrderId(id);
    }
    @Override
    public void edit1(Map map,Integer setmeals) {

        System.out.println(map);
        System.out.println(setmeals);

        try {
            String orderid = (String) map.get("id");

            //根据order id查询member id
            Integer memberId = orderDao.findMemberId(orderid);
            Member member=new Member();
            member.setId(memberId);
            String name = (String) map.get("name");
            member.setName(name);
            memberDao.edit(member);
//
            //  String setmeal = setmeals[0].toString();
            String id = (String) map.get("id");

            map.put("id",Integer.parseInt(id));
            map.put("setmealid",Integer.parseInt(String.valueOf(setmeals)));
            orderDao.edit(map);
        }catch (Exception e){
            System.out.println("****************************************************************");
            e.printStackTrace();
        }


    }

   /* @Override
    public Result edit(Integer order_d) {
        return orderSettingDao.edit(order_d);
    }*/
}